import { FC } from "react"

import { ShortcutOptionsType } from "@/components/common/timeSelectionShortcuts/types"
import { useLocale } from "@/locales"
import { rangeBeginning, rangeCurrentMonth, rangePastNDays, rangePastNMonth, rangeToday, rangeYesterday } from "@/utils"

import TimeRange from "../../common/timeRange"

const StartEndRange: FC<any> = ({
	setFieldValue,
	defaultValue,
	shortcuts,
	format = "YYYY-MM-DD",
	startFieldName = "startDate",
	endFieldName = "endDate",
	fieldName = "startEndTime",
	type = undefined,
	isAllowedAllTime = true,
	...rest
}) => {
	const { formatMessage } = useLocale()

	const defaultshortcuts: ShortcutOptionsType[] = [
		{
			label: formatMessage({ id: "common.utils.today" }),
			value: rangeToday()
		},
		{
			label: formatMessage({ id: "common.utils.yesterday" }),
			value: rangeYesterday()
		},
		{
			label: formatMessage({ id: "common.utils.last_n_days" }, { n: 7 }),
			value: rangePastNDays(7)
		},
		{
			label: formatMessage({ id: "common.utils.last_n_days" }, { n: 14 }),
			value: rangePastNDays(14)
		},
		{
			label: formatMessage({ id: "common.utils.last_n_days" }, { n: 30 }),
			value: rangePastNDays(30)
		},
		{
			label: formatMessage({ id: "common.utils.last_month" }),
			value: rangePastNMonth(1)
		},
		{
			label: formatMessage({ id: "common.utils.this_month" }),
			value: rangeCurrentMonth()
		},
		{
			label: formatMessage({ id: "common.utils.month3_recent" }),
			value: rangePastNDays(90)
		}
	]

	const presets = Array.isArray(shortcuts) ? shortcuts : defaultshortcuts

	if (isAllowedAllTime)
		presets.push({
			label: formatMessage({ id: "common.utils.select_all_range" }),
			value: rangeBeginning()
		})

	return type === "month" ? (
		<TimeRange
			presets={presets}
			setFieldValue={setFieldValue}
			noLabel={true}
			fieldName={fieldName}
			startFieldName={startFieldName}
			endFieldName={endFieldName}
			format={format}
			defaultValue={defaultValue}
			picker={type}
			{...rest}
		/>
	) : (
		<TimeRange
			presets={presets}
			setFieldValue={setFieldValue}
			noLabel={true}
			fieldName={fieldName}
			startFieldName={startFieldName}
			endFieldName={endFieldName}
			format={format}
			defaultValue={defaultValue}
			type={type}
			{...rest}
		/>
	)
}

export default StartEndRange
